<?php
declare (strict_types = 1);

namespace app\wechat\controller;

use app\admin\controller\AdminController;
use app\Request;
use app\wechat\model\ApplicationModel;
use app\wechat\service\ApplicationService;
use think\facade\View;

class Application extends AdminController
{
    /**
     * 应用列表
     *
     * @Author tanruizheng
     * @DateTime 2021-07-09
     * @return void
     */
    public function index(Request $request){
        if($request->param('_action') == 'getApplicationList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $adminList = ApplicationService::getApplicationList($params,$page,$limit);
            return json($adminList);
        }
        return View::fetch('index',[
           
        ]);
    }

    /**
     * 应用添加
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function add(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = ApplicationService::addOrEditData($params);
            return json($result);
        }
        return View::fetch('add',[
           
        ]);
    }

    /**
     * 应用编辑
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function edit(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $result = ApplicationService::addOrEditData($params);
            return json($result);
        }
        $id = $request->param('id',0);
        $data = ApplicationModel::where('id',$id)->find()->toArray();
        return View::fetch('edit',[
            'data' => $data
        ]);
    }

    /**
     * 应用删除
     *
     * @Author tanruizheng
     * @DateTime 2021-07-13
     * @param Request $request
     * @return void
     */
    public function delete(Request $request){
        $id = $request->param('id',0);
        if(!$id){
            return self::createFail('参数错误');
        }
        $result = ApplicationService::applicationDelete($id);
        return json($result);
    }
}
